/**
 *
 */
package com.shop.cloud.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;

/**
 * <p>
 * 时间、字符串转换工具
 * </p>
 */
public class DateUtil {
	public final static DateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); 

	public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
	private static final String SDF_1_REG = "^\\d{2,4}\\-\\d{1,2}\\-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}$";
	public static final SimpleDateFormat SDF_1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final String SDF_2_REG = "^\\d{2,4}\\-\\d{1,2}\\-\\d{1,2}$";
	private static final SimpleDateFormat SDF_2 = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
	private static final String SDF_3_REG = "^\\d{2,4}\\/\\d{1,2}\\/\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}$";
	private static final SimpleDateFormat SDF_3 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	private static final String SDF_4_REG = "^\\d{2,4}\\/\\d{1,2}\\/\\d{1,2}$";
	private static final SimpleDateFormat SDF_4 = new SimpleDateFormat("yyyy/MM/dd");
	private static final String SDF_5_REG = "^\\d{2,4}\\/\\d{1,2}\\/\\d{1,2} \\d{1,2}:\\d{1,2}$";
	private static final SimpleDateFormat SDF_5 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
	private static final String SDF_6_REG = "^\\d{2,4}\\-\\d{1,2}\\-\\d{1,2} \\d{1,2}:\\d{1,2}$";
	private static final SimpleDateFormat SDF_6 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	
	private static final String[] MONTHS = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};

	public static String filePath(String file, String type) {
		Calendar cal = Calendar.getInstance();
		int day = cal.get(Calendar.DATE);
		int month = cal.get(Calendar.MONTH) + 1;
		int year = cal.get(Calendar.YEAR);
		StringBuffer buff = new StringBuffer();
		buff.append(file);
		buff.append("/").append(type);
		buff.append("/").append(year).append("/").append(month).append("/").append(day);
		return buff.toString();
	}

	public static String filePath(String file) {
		Calendar cal = Calendar.getInstance();
		int day = cal.get(Calendar.DATE);
		int month = cal.get(Calendar.MONTH) + 1;
		int year = cal.get(Calendar.YEAR);
		StringBuffer buff = new StringBuffer();
		buff.append(file);
		buff.append("/").append(year).append("/").append(month).append("/").append(day);
		return buff.toString();
	}

	public static String fileName(String name, String type) {
		StringBuffer buff = new StringBuffer();
		buff.append(name).append("_").append(type);
		return buff.toString();
	}

	/**
	 * <p>
	 * 将日期字符串解析成日期对象，支持以下格式
	 * <li>yyyy-MM-dd HH:mm:ss
	 * <li>yyyy-MM-dd
	 * <li>yyyy/MM/dd HH:mm:ss
	 * <li>yyyy/MM/dd
	 * <li>yyyy-MM-dd HH:mm
	 * <li>yyyy/MM/dd HH:mm
	 * </p>
	 *
	 * @param str
	 * @return
	 */
	public static Date parse(String str) {
		if (str == null) {
			return null;
		}
		if (str.indexOf(".") > 0) {
			str = str.substring(0, str.indexOf(".")); // 截掉微秒
		}
		List<String> sdfRegArray = Arrays.asList(SDF_1_REG, SDF_2_REG, SDF_3_REG, SDF_4_REG, SDF_5_REG, SDF_6_REG);
		List<SimpleDateFormat> sdfArray = Arrays.asList(SDF_1, SDF_2, SDF_3, SDF_4, SDF_5, SDF_6);
		Date date = null;
		try {
			for (int index = 0; index < sdfRegArray.size(); index++) {
				if (Pattern.compile(sdfRegArray.get(index)).matcher(str).matches()) {
					return sdfArray.get(index).parse(str);
				}
			}
		} catch (ParseException e) {
			throw new RuntimeException("非法日期字符串，解析失败：" + str, e);
		}
		return date;
	}

	public static Date now() {
		Calendar gca = Calendar.getInstance(Locale.CHINA);
		return gca.getTime();
	}
	
	public static String convertNow() {
		Calendar gca = Calendar.getInstance(Locale.CHINA);
		return SDF_1.format(gca.getTime());
	}

	/**
	 * <p>
	 * 将日期格式化成字符串：yyyy-MM-dd
	 * </p>
	 *
	 * @param date
	 * @return
	 */
	public static String format(Date date) {
		if (date == null) {
			return null;
		}
		return new SimpleDateFormat(DEFAULT_DATE_PATTERN).format(date);
	}
	
	/**
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date parseDate(String dateStr,String pattern) {
		try {
			return new SimpleDateFormat(pattern).parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * <p>
	 * 将日期格式化成字符串：yyyy-MM-dd HH:mm:ss
	 * </p>
	 */
	public static String formatCare2Second(Date date) {
		if (date == null) {
			return null;
		}
		return SDF_1.format(date);
	}

	/**
	 * <p>
	 * 将日期格式化成相应格式的字符串，如：
	 * <li>yyyy-MM-dd HH:mm:ss
	 * <li>yyyy-MM-dd
	 * <li>yyyy/MM/dd HH:mm:ss
	 * <li>yyyy/MM/dd
	 * </p>
	 */
	public static String format(Date date, String pattern) {
		if (date == null) {
			return null;
		}
		if (pattern == null || "".equals(pattern)) {
			pattern = DEFAULT_DATE_PATTERN;
		}
		return new SimpleDateFormat(pattern).format(date);
	}

	/**
	 * 获取当天所代表的一周的开始
	 */
	public static String getWeekStart(Date date, String pattern) {
		if (date == null) {
			return null;
		}
		if (pattern == null || "".equals(pattern)) {
			pattern = DEFAULT_DATE_PATTERN;
		}
		Calendar cal = Calendar.getInstance();
		int n = 1;
		cal.setTime(date);
		cal.add(Calendar.DATE, -n * 7);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return new SimpleDateFormat(pattern).format(date);
	}

	/**
	 * 获取当天所代表的一周的结束
	 */
	public static String getWeekEnd(Date date, String pattern) {
		if (date == null) {
			return null;
		}
		if (pattern == null || "".equals(pattern)) {
			pattern = DEFAULT_DATE_PATTERN;
		}
		Calendar cal = Calendar.getInstance();
		int n = 1;
		cal.setTime(date);
		cal.add(Calendar.DATE, n * 7);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return new SimpleDateFormat(pattern).format(date);
	}
	

	
	/**
	 * 获取指定日期前N天的日期
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date getLastDate(Date date,int days) {  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        calendar.add(Calendar.DAY_OF_MONTH, -days);  
        date = calendar.getTime();  
        return date;  
    }
	
	/**
	 * 获取指定日期前N天的日期字符串
	 * @param date
	 * @param days
	 * @return
	 */
    public static String getLastDay(Date date,int days) {  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        calendar.add(Calendar.DAY_OF_MONTH, -days);  
        date = calendar.getTime();  
        return DEFAULT_DATE_FORMAT.format(date);  
    }  
    
    /**
     * 获取当前日期字符串
     * @return
     */
    public static Date getCurrentTime() {  
        Calendar calendar = Calendar.getInstance();  
        calendar.add(Calendar.DAY_OF_MONTH, 0);  
        return calendar.getTime();  
    }  
    
    /**
     * 获取当前日期字符串
     * @return
     */
    public static String getCurrentDay() {  
        Calendar calendar = Calendar.getInstance();  
        calendar.add(Calendar.DAY_OF_MONTH, 0);  
        Date date = calendar.getTime();  
        return DEFAULT_DATE_FORMAT.format(date);  
    }  
    
    /**
     * 获取指定日期后N天的日期
     * @param date
     * @param days
     * @return
     */
    public static Date getNextDate(Date date,int days) {  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        calendar.add(Calendar.DAY_OF_MONTH, days);  
        date = calendar.getTime();  
        return date;  
    }  
    
    /**
     * 获取指定日期后N天的日期字符串
     * @param date
     * @param days
     * @return
     */
    public static String getNextDay(Date date,int days) {  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        calendar.add(Calendar.DAY_OF_MONTH, days);  
        date = calendar.getTime();  
        return DEFAULT_DATE_FORMAT.format(date);  
    } 
    
    public static String getFlightDate(Date date) {  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        date = calendar.getTime();
        return date.getDate()+MONTHS[date.getMonth()]+DEFAULT_DATE_FORMAT.format(date).substring(2, 4);
    }
    
    
    public static void main(String[] args) {
    	System.out.println(getFlightDate(new Date()));
	}
}
